﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Chamberlain
{
    public partial class FrmApoderado : Form
    {
        int tipo = 0;
        FrmAlumno fra;
        Apoderado apo = new Apoderado();

        public FrmApoderado()
        {
            InitializeComponent();
            btnBuscar.Image = Properties.Resources.flecha;
      
        }
        public FrmApoderado(FrmAlumno fr)
        {
            InitializeComponent();
            btnBuscar.Image = Properties.Resources.flecha2;
            fra = new FrmAlumno();
            fra = fr;
            apo = fra.Apo;
            txtDNI.Text = apo.Dni;
            groupBox2.Enabled = false;            
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (txtDNI.Text != "")
            {
                apo.Dni = txtDNI.Text;
                apo.read();
                if (apo.Id != 0)
                {
                    txtNombre.Text = apo.Nombre;
                    txtApellido.Text = apo.Apellidos;
                    txtEmail.Text = apo.Email;
                    pictureBox2.Image = Properties.Resources.check;
                    btnBuscar.Image = Properties.Resources.flecha2;
                    groupBox2.Enabled = false;
                    tipo = 1;
                    btnGuardar.Focus();
                }
                else
                {
                    pictureBox2.Image = Properties.Resources.cross;
                    txtDNI.Focus();
                }
            }
            else 
            {
                pictureBox2.Image = Properties.Resources.cross;
                txtDNI.Focus();
                txtDNI.BackColor = Color.FromArgb(255, 255, 192);
                txtNombre.BackColor = Color.FromArgb(255, 255, 192);
                txtApellido.BackColor = Color.FromArgb(255, 255, 192);
                txtEmail.BackColor = Color.FromArgb(255, 255, 192);
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (validar() == true)
            {
                apo.Email = txtEmail.Text;
                apo.Nombre = txtNombre.Text;
                apo.Apellidos = txtApellido.Text;
                
                if (fra == null && tipo == 1)
                {                    
                    apo.update();
                    limpiarCampos();
                    groupBox2.Enabled = true;
                    btnBuscar.Image = Properties.Resources.flecha;
                }
                else
                {   
                    apo.Dni = txtDNI.Text;
                    apo.insert();
                    limpiarCampos();
                    txtDNI.Focus();
                    if (fra != null)
                    {
                        fra.Apo = apo;
                        
                        this.Close();
                    }        
                }
            }
        }

        private void limpiarCampos()
        {
            txtDNI.Text = "";
            txtNombre.Text = "";
            txtApellido.Text = "";
            txtEmail.Text = "";
        }

        private bool validar()
        {
            Apoderado auxApo = new Apoderado();
            if (fra == null)
            {
                if (txtDNI.Text == "")
                    txtDNI.BackColor = Color.FromArgb(255, 255, 192);
                else
                {
                    txtDNI.BackColor = Color.White;
                    auxApo.Dni = txtDNI.Text;
                    auxApo.read();
                    if (tipo == 0 && auxApo.Id != 0)
                        btnGuardar_Click(this,EventArgs.Empty);
                }
            }

            if (txtNombre.Text=="")
                txtNombre.BackColor = Color.FromArgb(255, 255, 192);
            else txtNombre.BackColor = Color.White;
            if (txtApellido.Text=="")
                txtApellido.BackColor = Color.FromArgb(255, 255, 192);
            else txtApellido.BackColor = Color.White;
            
            if (txtEmail.Text == "")
                txtEmail.BackColor = Color.FromArgb(255, 255, 192);
            else txtEmail.BackColor = Color.White;


            
            if ((txtDNI.Text == "") || (txtNombre.Text == "") ||
                (txtApellido.Text == "") || (txtEmail.Text == ""))
            { MessageBox.Show("porfavor, rellene todos los campos");
            return false;
            }
            else
            {                
                return true;
            }
        }
    }
}
